#include "userinfoservice.h"
#include <QSqlQuery>

UserInfoService::UserInfoService(QObject *parent):QObject(parent)
{

}

QString UserInfoService::getCurrentUser()
{
    QString userName;
    QString sql="SELECT UserGUID,UserName FROM UserInfo ";
    QSqlQuery query;
    query.exec(sql);
    if(query.exec())
    {
        while(query.next())
        {
            userName=query.value(1).toString();
            break;
        }
    }

    return userName;
}

QSqlQueryModel* UserInfoService::getUserModel()
{
    QString sql="SELECT UserGUID,UserName,RealName,Department,UserType FROM UserInfo ";
    QSqlQueryModel *model=new QSqlQueryModel;
    model->setQuery(sql);

    return model;
}

bool UserInfoService::update(QMap<QString, QString> map)
{
    QSqlQuery query;
    query.prepare("UPDATE UserInfo SET UserName=:username,RealName=:realname, "
                  "Department=:department,UserType=:usertype "
                  "WHERE UserGUID=:userguid");
    QString userguid=map["userguid"];
    query.bindValue(":username",map["username"]);
    query.bindValue(":realname",map["realname"]);
    query.bindValue(":department",map["department"]);
    query.bindValue(":usertype",map["usertype"]);
    query.bindValue(":userguid",userguid);
    bool blResult= query.exec();

    return blResult;
}

bool UserInfoService::insert(QMap<QString, QString> map)
{
    QSqlQuery query;
    query.prepare("INSERT INTO UserInfo (UserGUID,UserName,RealName,Department,Pwd,UserType) "
                  "VALUES (:userguid, :username,:realname,:department,:pwd,:usertype) ");
    QString userguid=map["userguid"];
    query.bindValue(":username",map["username"]);
    query.bindValue(":realname",map["realname"]);
    query.bindValue(":department",map["department"]);
    query.bindValue(":pwd",map["pwd"]);
    query.bindValue(":usertype",map["usertype"]);
    query.bindValue(":userguid",userguid);
    bool blResult= query.exec();

    return blResult;
}

bool UserInfoService::del(QString userguid)
{
    QSqlQuery query;
    query.prepare("DELETE FROM  UserInfo WHERE UserGUID=:userguid ");
    query.bindValue(":userguid",userguid);
    bool blResult= query.exec();

    return blResult;
}
